package com.yx.services.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yx.entity.GoodsEntity;
import com.yx.entity.Stock;
import com.yx.mapper.GoodsMapper;
import com.yx.mapper.StockMapper;
import com.yx.services.IGoodsService;
import com.yx.vo.Goods;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Classname GoodsServiceImpl
 * @Date 2021/07/08 16:04
 * @Author by yx
 * @Description TODO:
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsEntity> implements IGoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private StockMapper stockMapper;

    @Override
    @Transactional //开启事务
    public Goods saveGoodsAndStock(Goods goods) {
        // 1.将vo Goods分拆
        GoodsEntity goodsEntity = new GoodsEntity();
        //按属性赋值
        BeanUtils.copyProperties(goods, goodsEntity);
        //将赋值后的数据添加到goods表
        goodsMapper.insert(goodsEntity);

        Stock stock = new Stock();
        //使用回填的id
        stock.setGid(goodsEntity.getId());
        //vo的goods才有stock
        stock.setStock(goods.getGstock());
        stockMapper.insert(stock);
        //将回填的id赋值给vo实体类goods,es中需要使用数据库中的id,做为es的id
        goods.setId(goodsEntity.getId());
        return goods;
    }
}
